package ui;

import java.util.ArrayList;
import java.util.List;

import ui.IngresoCaptura;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Selector;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.commons.utils.ApplicationContext;
import org.uqbar.commons.utils.Home;

import Aplicacion.Captura;
import Homes.CapturasHome;

@SuppressWarnings("serial")
public class IngresarPatenteWindow extends SimpleWindow<IngresoCaptura> {
	
	public static final String INGRESARPATENTE = "ingresarPatente";
	
	public IngresarPatenteWindow(WindowOwner parent, CapturasHome capturasHome) {
		super(parent, new IngresoCaptura(capturasHome));
	}

	@Override
	protected void addActions(Panel panel) {
		new Button(panel)
			.setAsDefault()
			.disableOnError()			
			.setCaption("Confirmar")
			.onClick(new MessageSend(this, IngresarPatenteWindow.INGRESARPATENTE));
	}	

	@Override
	protected void createFormPanel(Panel panel) {
		Panel fields = new Panel(panel);
		fields.setLayout(new ColumnLayout(2));

		new Label(fields).setText("Patente");
		new TextBox(fields)
			.bindValueToProperty(IngresoCaptura.PATENTE);
		new Label(fields).setText("Captura");
		new Selector(fields)
			.setContents(this.getList(Captura.class), "descripcion")
			.bindValueToProperty(IngresoCaptura.CAPTURA);
	}
	
	public void ingresarPatente() {
		getModel().ingresarPatenteManual();
		close();
	}	
	
	private <T> List<T> getList(Class<T> type) {
		Home<T> home = ApplicationContext.getInstance().getHome(type);

		List<T> objects = new ArrayList<T>();
		objects.addAll(home.getObjects());

		return objects;
	}

}
